package com.xyq.fs.base;

import java.io.IOException;
import java.nio.file.Paths;

import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CharArraySet;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.LogByteSizeMergePolicy;
import org.apache.lucene.index.LogMergePolicy;
import org.apache.lucene.store.FSDirectory;
import com.xyq.fs.constants.FieldConstant;
import com.xyq.fs.constants.IndexStringConstants;
import com.xyq.fs.views.WindowsHelp;



public class MyDirectoryS {

	private static String dirName = "lucenedirs2";
	private static FSDirectory dir;
	public static IndexWriter writer;
	private static IndexWriterConfig writerConfig;
	/**
	 * 这个read主要是为了全盘索引用的
	 */
	public static IndexReader reader;
	/**
	 * 这个是自定义过滤表，没有过滤任何词汇，保证完全可搜索
	 */
	private static Set<String> stopWords = new HashSet<>();
	private static CharArraySet cs = new CharArraySet(stopWords, true);
	private static Analyzer analyzer = new StandardAnalyzer(cs);
	public static IndexSearcher searcher;

	public static void init() {

	}

	/**
	 * 初始化
	 */
	static {
		try {
			dir = FSDirectory.open(Paths.get(System.getProperty("user.dir") + "\\CONF\\" + dirName));
			writerConfig = new IndexWriterConfig(analyzer);
			LogMergePolicy mergePolicy = new LogByteSizeMergePolicy();
			writerConfig.setRAMBufferSizeMB(1);
			mergePolicy.setMergeFactor(2);
			writerConfig.setMergePolicy(mergePolicy);
			writerConfig.setRAMPerThreadHardLimitMB(1);
			writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
			writerConfig.setUseCompoundFile(false);
			writer = new IndexWriter(dir, writerConfig);

			// 第一次索引后如果索引文件巨大会卡顿，用线程
			new Thread(() -> {
				try {
					writer.commit();
					reader = DirectoryReader.open(writer);
					searcher = new IndexSearcher(reader);
					IndexStringConstants.init();
					WindowsHelp.init();
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			}).start();
					
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static IndexSearcher getIndexSearcher() {

		if (searcher == null)
			return new IndexSearcher(reader);
		return searcher;
	}

	public static IndexReader getRtIndexReader() {

		try {
			writer.flush();
			reader = DirectoryReader.open(writer);
			return reader;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reader;
	}

	public static void updateReaderAndSearcher() {

		new Thread(()->{
			getRtIndexReader();
			searcher = new IndexSearcher(reader);
		}).start();
		
	}

	/***
	 * 这个操作其实就是更新操作
	 * 
	 * @param doc
	 */
	public static void addDocument(Document doc) {

		try {
			Term term = new Term(FieldConstant.FILE_PATH, doc.get(FieldConstant.FILE_PATH));
			writer.updateDocument(term, doc);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void commit() {

		new Thread(() -> {
			try {
				writer.flush();
				writer.commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();
	}

	public static void closeAll() {

		if (reader != null)
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		if (writer.isOpen())
			try {
				writer.flush();
				writer.commit();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
}
